﻿using System.IO;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(SerializeRoot), true)]
public class SerializeRootEditor : Editor
{
    static SerializeRoot gizmos;
    SerializeConfig serializeCfg;

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        gizmos = target as SerializeRoot;
        serializeCfg = SerializeConfig.Singleton;

        if (gizmos.needRemove)
        {
            gizmos.FlushData();
        }
        else { gizmos.CheckData(); }

        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("ExportCS"))
        {
            SaveAndAttachScript(true);
        }
        if (GUILayout.Button("ExportLua"))
        {
            SaveAndAttachScript(false);
        }

        EditorGUI.BeginChangeCheck();
        if (GUILayout.Button(gizmos.SerProperty))
        {
            if (!Application.isPlaying)
            {
                gizmos.OperateNode(); 
            }
        }
        EditorGUILayout.EndHorizontal();

        gizmos.maskIdx = EditorGUILayout.MaskField("选择组件", gizmos.maskIdx, gizmos.names);
        foreach (var serialize in gizmos.childs)
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField("go:", GUILayout.Width(30));
                EditorGUILayout.ObjectField(serialize.go, typeof(GameObject), false);
                gizmos.GetComTypeEnum(serialize.go, out string[] comNames);

                serialize.maskIdx = EditorGUILayout.MaskField(serialize.maskIdx, comNames);
                var node = serialize.go.GetComponent<SerializeNode>();
                if (node) { node.maskIdx = serialize.maskIdx; }
            }
        }

        DrawProperties();
        if (EditorGUI.EndChangeCheck())
        {
            serializedObject.ApplyModifiedProperties();
        }
    }

    void DrawProperties()
    {
        var keyObjects = gizmos.GetKeyObjectDatas();
        foreach (var serialize in keyObjects)
        {
            using (new EditorGUILayout.HorizontalScope())
            {
                EditorGUILayout.LabelField("name:", GUILayout.Width(40));
                EditorGUILayout.SelectableLabel(serialize.name, GUILayout.Height(20));
                EditorGUILayout.LabelField("com:", GUILayout.Width(40));
                EditorGUILayout.ObjectField(serialize.com, typeof(Component), false);
            }
        }
    }

    void SaveAndAttachScript(bool script)
    {
        gizmos.ClassName = gizmos.name + serializeCfg.ClassSuffix;
        string scriptName = serializeCfg.GetTemplateFile(script, serializeCfg.FindGenerate);
        string fullPath = serializeCfg.GetScriptPath(script, out string scriptExt);

        fullPath = Application.dataPath + fullPath;
        fullPath = EditorUtility.SaveFilePanel("Create Script", fullPath, gizmos.ClassName, scriptExt);
        if (string.IsNullOrEmpty(fullPath)) { return; }

        gizmos.ClassName = Path.GetFileNameWithoutExtension(fullPath);
        if (gizmos.IsValidName(gizmos.ClassName))
        {
            gizmos.SaveScript(fullPath, serializeCfg.tempPath + scriptName, script, serializeCfg.FindGenerate);
            Debug.Log("Create Script == " + fullPath); AssetDatabase.Refresh();
        }
        else
        {
            EditorUtility.DisplayDialog("Invalid class name", "You have chosen a class name " +
               "that cannot be used as a valid identifier for a MonoBehavior", "CANCEL");
        }
    }
}